package com.itheima.mm.controller;

import com.itheima.mm.dto.CourseDTO;
import com.itheima.mm.dto.TagDTO;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.entity.TagPageQueryParams;
import com.itheima.mm.service.TagService;
import com.itheima.mm.vo.TagVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description TagController
 * @Author chunbo
 * @Date 2023-09-03
 */
@RestController
@RequestMapping("/tag")
@Slf4j
@Api(tags = "学科标签相关接口")
public class TagController {

    @Resource
    private TagService tagService;
//    学科标签添加
    @ApiOperation("学科标签添加接口")
    @PostMapping("/add.do")
    public Result add(@RequestBody TagDTO tagDTO){

        log.info("开始执行学科标签添加：{}",tagDTO);
        //调用业务
        tagService.add(tagDTO);
        //返回数据
        return Result.success(null,"学科标签添加成功");
    }


//    学科标签分页列表
    @ApiOperation("学科标签分页列表接口")
    @PostMapping("/findListByPage.do")
    public Result<PageResult<TagVO>> findListByPage(@RequestBody QueryPageBean<TagPageQueryParams> queryPageBean){
        log.info("开始执行学科标签分页列表接口：{}",queryPageBean);

        //调用业务
        PageResult pageResult = tagService.findListByPage(queryPageBean);

        return Result.success(pageResult);
    }
//    学科标签删除
    @ApiOperation("学科标签删除接口")
    @GetMapping("/delete.do")
    public Result delete(Integer tagId){
        log.info("开始执行学科标签删除接口：{}",tagId);

        //调用业务
        tagService.deleteTag(tagId);

        //返回数据
        return Result.success(null,"学科标签删除成功");
    }
    //更新学科标签
    @PostMapping("/update.do")
    public Result update(@RequestBody TagDTO tagDTO){
        log.info("处理学科标签更新请求，请求参数：{}",tagDTO);
        tagService.update(tagDTO);

        //返回数据
        return Result.success();

    }
    //学科标签禁用
    @PostMapping("/disable")
    public ResponseEntity<String> disableTag(@RequestBody Map<String, Object> params) {
        Integer id = (Integer) params.get("id");
        if (id == null) {
            return ResponseEntity.badRequest().body("无效的标签ID");
        }
        boolean result =tagService.disableTag(id);
        return result ? ResponseEntity.ok("目录禁用成功") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("目录禁用失败");
    }
    //学科标签启用
    @PostMapping("/enable")
    public ResponseEntity<String> enableCatalog(@RequestBody Map<String, Object> params) {
        Integer id = (Integer) params.get("id");
        if (id == null) {
            return ResponseEntity.badRequest().body("无效的目录ID");
        }
        boolean result = tagService.enableCatalog(id);
        return result ? ResponseEntity.ok("目录启用成功") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("目录启用失败");
    }
}